package com.woniuxy.qiantai.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.dal.entity.Collect;
import com.woniuxy.dal.entity.History;
import com.woniuxy.dal.entity.Ranges;
import com.woniuxy.dal.mapper.CollectMapper;
import com.woniuxy.dal.mapper.RangeMapper;
import com.woniuxy.dal.vo.CollectVo;
import com.woniuxy.servicelayer.CollectService;
import com.woniuxy.servicelayer.RangeService;
import com.woniuxy.servicelayer.util.JwtUtil;
import com.woniuxy.servicelayer.util.UserTokenUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.woniuxy.qiantai.model.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 收藏表(Collect)表控制层
 *
 * @author makejava
 * @since 2023-03-27 22:21:38
 */
@RestController
@RequestMapping("collect")
public class CollectController {
    /**
     * 服务对象
     */
    @Resource
    private CollectService collectService;

    @Resource
    private CollectMapper collectMapper;

    @Resource
    private RangeService rangeService;


    @ApiOperation("查询收藏")
    @RequestMapping("searchCollection")
    public Page<Collect> pageSearch( @RequestBody Map<String,Object> params) {

        Object token = params.get("token");
        Integer userId = (Integer) JwtUtil.parseValue((String) token, "userId");
        Integer currentPage = (Integer) params.get("currentPage");
        Integer pageSize = (Integer) params.get("pageSize");

        Page<Collect> collectPage = collectService.pageSearch(currentPage,pageSize,userId);
        return collectPage;
    }


    @ApiOperation("添加收藏")
    @RequestMapping("addFavorite")
    public Result<String> addFavorite(@RequestBody Map<String, Object> params){
        Collect collect = new Collect();
        Object token = params.get("token");
        Integer userId = (Integer) JwtUtil.parseValue((String) token, "userId");
        Integer rangeId = (Integer) params.get("rangeId");
        Ranges ranges = new Ranges();
        ranges = rangeService.getRangeById(rangeId);
        collect.setUserId(userId);
        collect.setRangeName(ranges.getRangeName());
        collect.setRangeDetail(ranges.getRangeDetail());
        collect.setRangeId(rangeId);
        collect.setCreateTime(new Date());
        collectService.addFavorite(collect);
        return Result.successResult("ok");
    }

    @ApiOperation("删除收藏")
    @RequestMapping("cancelFavorite")
    public Result<String> deleteCollection(@RequestBody Map<String, Object> params){

        Object token = params.get("token");
        Integer userId = (Integer) JwtUtil.parseValue((String) token, "userId");
        Integer rangeId = (Integer) params.get("rangeId");
        collectService.cancelFavorite(rangeId,userId);

        return Result.successResult("ok");
    }



}

